/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package thermologic.file;

import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import thermologic.core.Datalogger;
import thermologic.core.Device;

/**
 *
 * @author mrivadeneira
 */
public class XMLFileHandler implements FileHandler
{

    public void save(File file, Object obj) throws IOException {
    
        FileOutputStream fos = null;
        
        if(file == null)
        {
            throw new IllegalArgumentException("file is null"); 
        }
        
        if(file.isDirectory())
        {
            throw new IllegalArgumentException(
                        String.format("File %s is a directory", file.getName())
                    );
        }
        
        try
        {   
            fos = new FileOutputStream(file);
            XMLEncoder xenc = new XMLEncoder(fos);
            
            Device[] devices = (Device[])obj;
            for(Device d: devices)
            {
                Datalogger datalogger = (Datalogger)d;
                xenc.writeObject(datalogger);
            }
            xenc.flush();
        }
        finally
        {
            
            fos.close();
        }
    }

    public Object load(File file) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
